#include <iostream>
#include <fstream>
using namespace std;
#include "contacts.pb.h"
void PrintContacts(contacts2::Contacts& contacts)
{
    for(int i = 0;i<contacts.people_size();i++)
    {
        cout<<"---------联系人"<<i+1<<"------"<<endl;

        const contacts2::PeopleInfo& people = contacts.people(i);
        cout<<"联系人姓名: "<<people.name()<<endl;
        cout<<"联系人年龄: "<<people.age()<<endl;
        for(int j = 0;j<people.phone_size();j++)
        {
            const contacts2::PeopleInfo_Phone& phone = people.phone(j);
            cout<<"联系人电话"<<j+1<<": "<<phone.number();
            //联系人1:1234    (MP)
            cout<<"   ("<< PeopleInfo_Phone_PhoneType_Name(phone.type())<<")"<<endl;
        }
        contacts2::Address address;
        if(people.has_data()&&people.data().Is<contacts2::Address>())
        {
            people.data().UnpackTo(&address);
            if(!address.home_address().empty())
                cout<<"联系人家庭地址: "<<address.home_address()<<endl;
            if(!address.unit_address().empty())
                cout<<"联系人单位地址: "<<address.unit_address()<<endl;
        }

        // if(people.has_qq())
        // { 
        // }
        // else if(peopel.has_wechat())
        // {
        // }
        switch(people.other_contact_case())
        {
            case contacts2::PeopleInfo::OtherContactCase::kQq:
                cout<<"qq: "<<people.qq()<<endl;
                break;
            case contacts2::PeopleInfo::OtherContactCase::kWechat:
                cout<<"微信: "<<people.wechat()<<endl;
                break;
            default:
                break;
        }

        cout<<"标签信息:"<<endl;
        if(people.remark_size()==0)
        {
            cout<<"   空"<<endl;
        }
        else
        {
            for(auto it = people.remark().begin();it!=people.remark().end();it++)
            {
                cout<<it->first<<": "<<it->second<<endl;
            }
        }

    }
}

int main()
{
    contacts2::Contacts contacts;
    //打开文件，读取通讯录的信息
    fstream input("contacts.bin",ios::in | ios::binary);//因为文件中存储的是二进制的信息
    if(!contacts.ParseFromIstream(&input))
    {
        cerr<<"parse error"<<endl;
        input.close();
        return -1;
    }
    //打印
    PrintContacts(contacts);
    input.close();
    return 0;
}